Sample Workflow for the zonalDaymet Package

This Vignette serves as an example workflow of the zonalDaymet package functions. The example includes precipitation and minimum temperature records over 2008 and 2009 in southeastern Massachusetts. Hydrologic catchment polygons are used as the zones over which to spatially average the climate records.

Functions not in this package are preceded by their package source and a "::". Functions in the zonalDaymet package are referred to directly.

Load Libraries

The devtools package is used to load the zonalDaymet package from GitHub. The maptools and sp packages are used to load and process the spatial objects.

library(devtools)
library(maptools)
library(sp)

install_github("Conte-Ecology/zonalDaymet")
library(zonalDaymet)

Download Daymet Mosaic Files

The downloadMosaic function connects to the THREDDS server used by Daymet to make the mosaic netCDF files avilable. Individual files are downloaded by specifying the years and variables to download as well as the file destination directory. There is also an option to retry failed downloads that become corrupted. This is a possibility if the connection is lost during the download. A progress bar will appear during the download as well as messages describing the existence of downloaded netCDF files and if any are corrupt.

downloadMosaic(years = 2008:2009,
                variables = c("tmin", "prcp"),
                destinationDirectory = "C:/CLIMATE/daymet/raw",
                retryFailedDownloads = TRUE)

Spatial Data Processing

The maptools package is used to load the spatial polygons layer. The shapefile should already be projected into the same coordinate system as the Daymet Projection. The projection argument mathces the PROJ.4 documentation for the Daymet spatial reference, which is in the Lambert Conformal Conic system.

zonesShapefile <- maptools::readShapePoly("C:/CLIMATE/daymet/spatial/Catchments01_Daymet.shp", 
                                            proj4string = CRS("+proj=lcc +ellps=WGS84 +datum=WGS84 +lat_1=25 +lat_2=60 +lat_0=42.5 +lon_0=-100 +x_0=0 +y_0=0"))

The spatial object is transformed into the geographic coordinate system using the sp package. This step ensures that the units are in degrees, making the object comparable to the coordinates provided by Daymet NetCDFs in the World Geodetic System (WGS_84).

transformShapefile <- sp::spTransform(zonesShapefile,
                                        CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs +towgs84=0,0,0"),
                                        class = "SpatialPolygonsDataFrame")

The transformed spatial object is split into segments using the tileShapefile function. This step is helpful with processing large and complex spatial objects that will likely exceed memory limitations if not broken into segments. Currently, the tile size options are 1 x 1 and 2 x 2 degrees.

tiledShapefile <- tileShapefile(shapefile = transformShapefile,
                                tileDegree = 1)

A single tile of catchments is selected for processing.

zonesPolygon <- tiledShapefile[[5]]

plot(zonesPolygon)

Climate Record Processing: Spatial Polygons

With the Daymet mosaics downloaded and the spatial objects loaded into R, the zonalDaymet functions are used to process the climate records. The assignZonalRecordsToDatabase function spatially maps the Daymet records to each polygon. Multiple records falling inside the polygon get averaged to output a single record. If no points fall inside the polygon, the nearest point is assigned. The function iterates through netCDF files based on the specified variables and years to process. Results are written to a SQLite database. If the database does not exist, it is created automatically.

assignZonalRecordsToDatabase(zonesShapefile = zonesPolygon,
                              zoneField = "FEATUREID",
                              zoneFieldType = "integer",
                              mosaicDirectory = "C:/CLIMATE/daymet/raw",
                              variables = c("tmin", "prcp"), 
                              years = 2008:2009,
                              databaseFilePath = "C:/CLIMATE/daymet/databases/exampleDatabase", 
                              databaseTableName = "climateRecord")

The returnZonalRecordsFromDatabase accesses a database and returns records based on specified dates and variables. If the user is intersted in the first snowfall of the season, one method may be to inspect the minimum temperature and precipitation records. Two of the polygons are selected to keep the output size relatively small.

zones <- unique(zonesPolygon@data$FEATUREID)[1:2]

exampleZoneDB <- returnZonalRecordsFromDatabase(databaseFilePath = "C:/CLIMATE/daymet/databases/exampleDatabase", 
                                                databaseTableName = "climateRecord",
                                                zoneField = "FEATUREID",
                                                startDate = "2008-10-01",
                                                endDate = "2008-12-31",
                                                zoneIDs = zones,
                                                variables = c("tmin", "prcp"))

head(exampleZoneDB)

The assignZonalRecordsToDataframe function works the same as assignZonalRecordsToDatabase, except that it writes the results to a dataframe in the R workspace. Options to specify the records are the same, which means that care must be taken to ensure that the output will not exceed memory limitations.

exampleZoneDF <- assignZonalRecordsToDataframe(zonesShapefile = zonesPolygon,
                                                zoneField = "FEATUREID",
                                                mosaicDirectory = "C:/CLIMATE/daymet/raw",
                                                variables = c("tmin", "prcp"), 
                                                years = 2008:2009)

zones <- unique(zonesPolygon@data$FEATUREID)[1:2]

selectZones <- exampleZoneDF[which(exampleZoneDF$FEATUREID %in% zones),]

head(selectZones)

Climate Record Processing: Coordinates

The returnRecordsByCoordinates function takes user defined boundaries and returns all of the records that fall within these bounds to a dataframe in the R workspace. It is important that the start and end dates of the record match with the netCDF mosaic file. Two output formats are available.

exampleCoordsLong <- returnRecordsByCoordinates(areaExtent = c(-71.0, -70.0, 42.0, 43.0),
                                                  mosaicFile = "C:/CLIMATE/daymet/raw/prcp_2008.nc4",
                                                  outputFormat = "long",
                                                  startDate = "2008-01-01",
                                                  endDate = "2008-01-31")

exampleCoordsWide <- returnRecordsByCoordinates(areaExtent = c(-71.0, -70.0, 42.0, 43.0),
                                                  mosaicFile = "C:/CLIMATE/daymet/raw/prcp_2008.nc4",
                                                  outputFormat = "wide",
                                                  startDate = "2008-01-01",
                                                  endDate = "2008-01-31")

Long format output:

str(exampleCoordsLong)

Wide format output:

head(exampleCoordsWide)[1:6]


Conte-Ecology/zonalDaymet documentation built on Aug. 11, 2021, 12:52 a.m.